package org.jeecg.modules.middleware.rabbitmq.gen.handler.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DictUtil;
import org.jeecg.modules.api.wlhy.setting.SendWxMessage;
import org.jeecg.modules.middleware.message.websocket.WebSocket;
import org.jeecg.modules.middleware.rabbitmq.gen.EnumGenHandler;
import org.jeecg.modules.middleware.rabbitmq.gen.handler.IGenMqHandle;
import org.jeecg.modules.wlhy.hyarea.entity.HyArea;
import org.jeecg.modules.wlhy.hyarea.service.IHyAreaService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.controller.HttpURLConnectionUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 消息
 */
@Service
public class WebSocketXtxxHandler implements IGenMqHandle {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketXtxxHandler.class);

    @Autowired
    private IHyOrderService orderService;
    @Autowired
    private  IHyDriverService driverService;

    @Autowired
    private IHyAreaService hyAreaService;

    @Autowired
    private DictUtil dictUtil;

    @Override
    public String getHandlerName() {
        return EnumGenHandler.WEBSOCKET_XXTS_HANDLER.getHandlerName();
    }

    @Override
    public void handle(String channel, String msg) {


        //wx消息订阅 和app消息穿透
        if(StringUtils.isNotEmpty(channel)){
            List<HyDriver> hyDriverList = driverService.list();
            HyOrder hyOrder = orderService.getOne(new QueryWrapper<HyOrder>().eq("order_num","HY202203251758582746"));
            HyArea sendAreaCode = getAreaNames(hyOrder.getSendAreaCode() + "000000000");
            HyArea receiveAreaCode = getAreaNames(hyOrder.getReceiveAreaCode() + "000000000");

            List<String> sendNames = Arrays.asList(sendAreaCode.getMergerName().split(","));
            List<String> receiveNames = Arrays.asList(receiveAreaCode.getMergerName().split(","));
            hyOrder.setSendAddressName(sendNames.get(1));
            hyOrder.setReceiveAddressName(receiveNames.get(1));
            if(hyOrder!=null){

                JSONObject objectDict = dictUtil.parseObjectDict(hyOrder);

                for (HyDriver driver : hyDriverList) {
                    if(StringUtils.isNotEmpty(driver.getWxOpenid()) ){
                        String push = SendWxMessage.push(driver.getWxOpenid(), objectDict);
                        System.out.println(push);
                    }

                }
            }
        }
    }

    private HyArea getAreaNames(String areaCode) {
        LambdaQueryWrapper<HyArea> queryWrapperArea = new LambdaQueryWrapper<>();
        queryWrapperArea.eq(HyArea::getAreaCode, areaCode);
        return hyAreaService.getOne(queryWrapperArea);
    }
}
